package com.chushouya.product.dao.repository;

import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.EvaluateItemEntity;
import com.chushouya.product.dao.mapper.EvaluateItemEntityMapper;
import com.chushouya.product.dto.admin.evaluateitem.EvaluateItemQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class EvaluateItemRepository extends BaseRepository<EvaluateItemEntityMapper, EvaluateItemEntity, Long> {
    
    public List<EvaluateItemEntity> selectList(EvaluateItemQuery query) {
        Example example = buildExample(query);
        return this.selectByExample(example);
    }

    public PageResult<EvaluateItemEntity> selectPageResult(EvaluateItemQuery query) {
        Example example = this.buildExample(query);
        return this.selectPage(query.getPageRequest(), example);
    }
    
    public EvaluateItemEntity selectOne(EvaluateItemQuery query) {
        Example example = buildExample(query);
        return this.selectFirstOneByExample(example);
    }

    private Example buildExample(EvaluateItemQuery query) {
        Example example = new Example(EvaluateItemEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(query.getEvaluateItemId())) {
            criteria.andEqualTo("evaluateItemId", query.getEvaluateItemId());
        }
        if (Objects.nonNull(query.getCategoryId())) {
            criteria.andEqualTo("categoryId", query.getCategoryId());
        }
        if (Strings.isNotEmpty(query.getCategoryName())) {
            criteria.andEqualTo("categoryName", query.getCategoryName());
        }
        if (Strings.isNotEmpty(query.getItemName())) {
            criteria.andEqualTo("itemName", query.getItemName());
        }
        if (Objects.nonNull(query.getSelectType())) {
            criteria.andEqualTo("selectType", query.getSelectType());
        }
        if (Objects.nonNull(query.getStatus())) {
            criteria.andEqualTo("status", query.getStatus());
        }
        if (Objects.nonNull(query.getCreateTimeStart())) {
            criteria.andGreaterThanOrEqualTo("createTime", query.getCreateTimeStart());
        }
        if (Objects.nonNull(query.getCreateTimeEnd())) {
            criteria.andLessThanOrEqualTo("createTime", query.getCreateTimeEnd());
        }
        example.orderBy("sort").asc();
        return example;
    }
    
    public List<EvaluateItemEntity> selectListByCategoryId(Long categoryId) {
        EvaluateItemQuery query = new EvaluateItemQuery();
        query.setCategoryId(categoryId);
        return selectList(query);
    }
    
    public List<EvaluateItemEntity> selectListByCategoryIds(List<Long> categoryIds) {
        if (Lists.isEmpty(categoryIds)) {
            return Lists.newArrayList();
        }
        Example example = new Example(EvaluateItemEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("categoryId", categoryIds);
        example.orderBy("sort").asc();
        return this.selectByExample(example);
    }
}
